package cn.campus.converter;

import cn.campus.pojo.article.dto.ArticleDTO;
import cn.campus.pojo.article.po.Article;
import org.springframework.beans.BeanUtils;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * Created by linyanbin on 2018/10/6.
 */
public class ArticleToArticleDTOConverter {
    /**
     * 将Article对象转成ArticleDTO对象
     * @param article
     * @return
     */
    public static ArticleDTO convert(Article article) {
        if (Objects.isNull(article)) {
            return null;
        }
        ArticleDTO articleDTO = new ArticleDTO();
        BeanUtils.copyProperties(article, articleDTO);
        return articleDTO;
    }

    /**
     * 将Article对象列表转成ArticleDTO对象列表
     * @param articleList
     * @return
     */
    public static List<ArticleDTO> convert(List<Article> articleList) {
        if (Objects.isNull(articleList) || articleList.size() == 0) {
            return null;
        }
        // 使用java8新特性: 流API，方法引用
        return articleList.parallelStream().map(ArticleToArticleDTOConverter::convert).collect(Collectors.toList());
    }
}
